<?php
namespace app\common\model;

use think\Db;

class UserMemberModel extends BaseModel
{

    protected $name = 'user_member'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改
    //类型自动转换
    protected $type = [];
    /**
     * 用户会员拷贝到站点会员
     * @param $memberUid
     * @param $siteData
     * @return bool
     */
    public function copyMemberToSite($memberUid,$siteData){
        $info = Db::table('user')->field('uid',true)->where('status',1)->where('uid',$memberUid)->where('utype','member')->find();
        if(!info){
            $this->error = '目标会员不存在或未通过审核！';
            return false;
        }
        $info = $info->toArray();
        if(!empty($siteData['password'])){
            $info['password'] = md5($siteData['password']);
        }
        $info['status'] = 0; //置为未审核
        $info['utype'] = 'site';
        $info['group_id'] = '2001'; //店铺创始人角色ID
        $uid = Db::table('user')->insertGetId($info);
        Db::table('user')->where('uid',$uid)->setField('site_id',$uid); //设置site_id
        $siteModel   = new siteModel();
        $siteData['uid'] = $uid;
        $siteModel->addData($siteData);
        return true;
    }

    /**
     * 检查member会员对应site站点
     * @param $memberUid
     * @return bool
     */
    public function checkSite($memberUid){
        $username = Db::table('user')->where('status',1)->where('uid',$memberUid)->where('utype','member')->value('username');
        if(empty($username)){
            $this->error = '会员不存在或未通过审核！';
            return false;
        }
        $map['username'] = $username;
        $map['utype'] = 'site';
        $siteInfo = Db::table('user')->where($map)->find();
        if(empty($siteInfo)){
            $this->error = '未找到对应站点';
            return false;
        }
        return $siteInfo;
    }
    /*读取器*/

    /*修改器*/

}